# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

add_library(
  mvfst_server_state STATIC
  handshake/AppToken.cpp
  handshake/ServerHandshake.cpp
  handshake/StatelessResetGenerator.cpp
  state/ServerStateMachine.cpp)

target_include_directories(
  mvfst_server_state PUBLIC
  $<BUILD_INTERFACE:${QUIC_FBCODE_ROOT}>
  $<BUILD_INTERFACE:${LIBFIZZ_INCLUDE_DIR}>
  $<INSTALL_INTERFACE:include/>
)

target_compile_options(
  mvfst_server_state
  PRIVATE
  ${_QUIC_COMMON_COMPILE_OPTIONS}
)

add_dependencies(
  mvfst_server_state
  mvfst_buf_accessor
  mvfst_cc_algo
  mvfst_constants
  mvfst_codec
  mvfst_codec_types
  mvfst_dsr_frontend
  mvfst_fizz_handshake
  mvfst_qlogger
  mvfst_state_ack_handler
)


add_library(
  mvfst_server STATIC
  QuicServer.cpp
  QuicServerBackend.cpp
  QuicServerPacketRouter.cpp
  QuicServerTransport.cpp
  QuicServerWorker.cpp
  CCPReader.cpp
  QuicCcpThreadLauncher.cpp
  SlidingWindowRateLimiter.cpp
  handshake/DefaultAppTokenValidator.cpp
  handshake/RetryTokenGenerator.cpp

  # Fizz specific parts, will be split in its own lib eventually.
  ../fizz/server/handshake/AppToken.cpp
  ../fizz/server/handshake/FizzServerQuicHandshakeContext.cpp
  ../fizz/server/handshake/FizzServerHandshake.cpp
)

target_include_directories(
  mvfst_server PUBLIC
  $<BUILD_INTERFACE:${QUIC_FBCODE_ROOT}>
  $<BUILD_INTERFACE:${LIBFIZZ_INCLUDE_DIR}>
  $<INSTALL_INTERFACE:include/>
)

target_compile_options(
  mvfst_server
  PRIVATE
  ${_QUIC_COMMON_COMPILE_OPTIONS}
)

add_dependencies(
  mvfst_server
  mvfst_buf_accessor
  mvfst_cc_algo
  mvfst_constants
  mvfst_codec
  mvfst_codec_types
  mvfst_dsr_frontend
  mvfst_fizz_handshake
  mvfst_qlogger
  mvfst_server_state
  mvfst_state_ack_handler
  mvfst_state_datagram_handler
  mvfst_transport
  mvfst_transport_knobs
)

if(DEFINED CCP_ENABLED)
  # This target is generated by running cargo, so cmake needs to know where
  # to find it. See quic/congestion_control/third_party/ccp/CMakeLists.txt
  # When CCP is not enabled, LIBSTARTCCP_LIBRARY is empty, and thus ignored
  # during linking.
  get_target_property(LIBSTARTCCP_LIBRARY libstartccp LOCATION)

  # This target is only defined when CCP_ENABLED, so we can ony add it here.
  add_dependencies(mvfst_server libstartccp)
  add_dependencies(mvfst_server_state libstartccp)
endif()

target_link_libraries(
  mvfst_server_state PUBLIC
  Folly::folly
  ${LIBFIZZ_LIBRARY}
  mvfst_buf_accessor
  mvfst_constants
  mvfst_codec
  mvfst_codec_types
  mvfst_dsr_frontend
  mvfst_fizz_handshake
  mvfst_qlogger
  mvfst_state_ack_handler
  mvfst_transport
  mvfst_transport_knobs
  ${LIBCCP_LIBRARY}
  ${LIBSTARTCCP_LIBRARY}
)

target_link_libraries(
  mvfst_server PUBLIC
  Folly::folly
  ${LIBFIZZ_LIBRARY}
  mvfst_buf_accessor
  mvfst_constants
  mvfst_codec
  mvfst_codec_types
  mvfst_dsr_frontend
  mvfst_fizz_handshake
  mvfst_qlogger
  mvfst_server_state
  mvfst_state_ack_handler
  mvfst_state_datagram_handler
  mvfst_transport
  mvfst_transport_knobs
  ${LIBCCP_LIBRARY}
  ${LIBSTARTCCP_LIBRARY}
)

file(
  GLOB_RECURSE QUIC_API_HEADERS_TOINSTALL
  RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
  *.h
)
list(FILTER QUIC_API_HEADERS_TOINSTALL EXCLUDE REGEX test/)
list(FILTER QUIC_API_HEADERS_TOINSTALL EXCLUDE REGEX async_tran/)
foreach(header ${QUIC_API_HEADERS_TOINSTALL})
  get_filename_component(header_dir ${header} DIRECTORY)
  install(FILES ${header} DESTINATION include/quic/server/${header_dir})
endforeach()

install(
  TARGETS mvfst_server
  EXPORT mvfst-exports
  DESTINATION lib
)

install(
  TARGETS mvfst_server_state
  EXPORT mvfst-exports
  DESTINATION lib
)

add_subdirectory(test)
add_subdirectory(handshake/test)
